package com.chandon.juc.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * CachedThreadPool特点：永远都有工作线程处理任务
 *
 * public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
 *
 *         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
 *                                       60L, TimeUnit.SECONDS,
 *                                       new SynchronousQueue<Runnable>(),
 *                                       threadFactory);
 * }
 *
 *
 * 最大线程数设置为了Integer.MAX_VALUE
 * 本质上还是普通线程池，延时功能基于DelayQueue，周期性执行任务，完毕后扔回阻塞队列。
 *
 * @author Chandon
 */

public class TestCachedThreadPool {
	public static void main(String[] args) {

		ExecutorService cachedThreadPool = Executors.newCachedThreadPool(new NamedThreadFactory("cachedThreadFactory"));


		for (int i = 1; i <= 100; i++) {

			final int a = i;
			cachedThreadPool.execute(() -> {
				String s = Thread.currentThread().getName() + " 任务" + a;
				System.out.println(s);
			});

		}
		cachedThreadPool.shutdown();

	}

}
